programming4us
           
 
 
SQL Server

SQL Server 2008: Administering Database Objects - Working with Tables (part 1) - Default Constraints

- Free product key for windows 10
- Free Product Key for Microsoft office 365
- Malwarebytes Premium 3.7.1 Serial Keys (LifeTime) 2019
2/8/2011 11:30:44 AM
The remaining objects discussed in this article are database objects, and they are located in folder structures under each individual database (with the exception of logon triggers). As you expand a database, you will encounter the Tables folder. A table is the most important object in the database because it allows you to store data in a logical, structured manner that can be retrieved and processed as needed. You can create up to 2 billion tables per database, and each table can have up to 30,000 columns. The number of rows each table can have is only limited by the available disk space on the server.

1. Default Constraints

You can define default constraints on columns within a table in order to insert a predefined value into those columns whenever a specific value is not provided. Each column can have only one default constraint. You can create the default when you create a table, or you can add it later using the ALTER TABLE statement. You cannot alter a default. To "change" a default, you must drop the existing constraint and create a new one.

To create a default on a column when you create a table, you can specify the constraint following the column definition. In Listing 1, we are creating a table called Orders with an OrderID and an OrderDate column. The OrderDate column has a default named DF_Orders_OrderDate. The column will be given the value of getdate() whenever a value is not otherwise specified.

Example 1. Syntax to Create a Default on a Column When Creating a Table
USE AdventureWorks2008
GO


CREATE TABLE Orders
(OrderID int identity,
OrderDate DateTime NOT NULL
CONSTRAINT [DF_Orders_OrderDate] DEFAULT (getdate()) )

If you want to add a default to a column in a table that already exists, you can use the ALTER TABLE statement, as shown in Listing 2. You will end up with the exact same table by running the code in Listing 9-5 or Listing 9-6.

Example 2. Syntax to Create a Constraint on a Preexisting Column
USE AdventureWorks2008
GO

--Drop the table if it currently exists
IF OBJECT_ID('dbo.Orders', 'U') IS NOT NULL
DROP TABLE dbo.Orders;

--Create the Orders table
CREATE TABLE Orders
(OrderID int identity ,
OrderDate DateTime NOT NULL)

--Alter the Orders table to add the default constraint
ALTER TABLE dbo.Orders
ADD CONSTRAINT [DF_Orders_OrderDate]
DEFAULT (getdate()) FOR OrderDate

You can also use the ALTER TABLE statement to add a new column with a default value, as shown in Listing 3. You can use the WITH VALUES clause to apply the default to any existing row in the table. If the WITH VALUES clause is not used, all of the preexisting rows will contain a NULL value, and the default will only be applied to new rows.

Example 3. Syntax to Add a Column to a Table with a Default Using the ADD VALUES Clause
USE AdventureWorks2008
GO


--Drop the table if it currently exists
IF OBJECT_ID('dbo.Orders', 'U') IS NOT NULL
DROP TABLE dbo.Orders;

--Create the Orders table
CREATE TABLE Orders
(OrderID int NOT NULL)

--Insert 3 records into the Orders table
INSERT INTO Orders (OrderID)
VALUES(1),(2),(3)

--Alter the table to add the default
ALTER TABLE dbo.Orders
ADD OrderDate Datetime NULL
CONSTRAINT [DF_Orders_OrderDate]
DEFAULT (getdate()) WITH VALUES

--Select to see the default was applied to the existing rows
SELECT OrderID, OrderDate FROM Orders

As you can see in Figure 1, the new default was applied to all three existing columns, giving each row the exact same OrderDate. If you remove the WITH VALUES clause from Listing 3, all of the order dates would be NULL. If you define the new OrderDate column as NOT NULL, you do not have to specify the WITH VALUES clause as it will be implied.

Figure 1. Result set returned from Listing 3

You can drop a default constraint by using the ALTER TABLE statement with the DROP CONSTRAINT clause, as shown in Listing 4.

Example 4. Code Used to Drop a Default Constraint
ALTER TABLE Orders DROP CONSTRAINT DF_Orders_OrderDate
Other -----------------
- SQL Server 2008: Administering Database Objects - Working with Database Snapshots
- Programming with SQL Azure : WCF Data Services (part 3)
- Programming with SQL Azure : WCF Data Services (part 2) - Creating the Client Application
- Using XML in SQL Server 2008: Relational Data As XML - The FOR XML Modes (part 4) - EXPLICIT Mode
- Using XML in SQL Server 2008: Relational Data As XML - The FOR XML Modes (part 3) - AUTO Mode
- Programming with SQL Azure : WCF Data Services (part 1)
- Using XML in SQL Server 2008: Relational Data As XML - The FOR XML Modes (part 2) - Working with Binary Columns
- Using XML in SQL Server 2008: Relational Data As XML - The FOR XML Modes (part 1) - RAW Mode
- Programming with SQL Azure : Connecting to SQL Azure (part 4) - Sqlcmd
- Programming with SQL Azure : Connecting to SQL Azure (part 3) - ODBC
- Programming with SQL Azure : Connecting to SQL Azure (part 2)
- Programming with SQL Azure : Connecting to SQL Azure (part 1) - ADO.NET
- Programming with SQL Azure : Application Deployment Factors
- SQL Server 2008: SQL Server Web Services - Building Web Services (part 3)
- SQL Server 2008: SQL Server Web Services - Building Web Services (part 2)
- SQL Server 2008: SQL Server Web Services - Building Web Services (part 1)
- SQL Server 2008: SQL Server Web Services
- SQL Server 2008: SQL Server Service Broker - Related System Catalogs
- SQL Azure Backup Strategies (part 2)
- SQL Azure Backup Strategies (part 1) - Copying a Database
 
 
 
Top 10
 
- Microsoft Visio 2013 : Adding Structure to Your Diagrams - Finding containers and lists in Visio (part 2) - Wireframes,Legends
- Microsoft Visio 2013 : Adding Structure to Your Diagrams - Finding containers and lists in Visio (part 1) - Swimlanes
- Microsoft Visio 2013 : Adding Structure to Your Diagrams - Formatting and sizing lists
- Microsoft Visio 2013 : Adding Structure to Your Diagrams - Adding shapes to lists
- Microsoft Visio 2013 : Adding Structure to Your Diagrams - Sizing containers
- Microsoft Access 2010 : Control Properties and Why to Use Them (part 3) - The Other Properties of a Control
- Microsoft Access 2010 : Control Properties and Why to Use Them (part 2) - The Data Properties of a Control
- Microsoft Access 2010 : Control Properties and Why to Use Them (part 1) - The Format Properties of a Control
- Microsoft Access 2010 : Form Properties and Why Should You Use Them - Working with the Properties Window
- Microsoft Visio 2013 : Using the Organization Chart Wizard with new data
- First look: Apple Watch

- 3 Tips for Maintaining Your Cell Phone Battery (part 1)

- 3 Tips for Maintaining Your Cell Phone Battery (part 2)
programming4us programming4us